//
//  WKShareView.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/24.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKShareView.h"
#define CONTENTHEIGHT 260.0f

@interface WKShareListCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *iconImgView;
@property(nonatomic,strong)UILabel *titleLb;
@end


@implementation WKShareListCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self layoutShareCell];
    }
    return self;
}

- (void)layoutShareCell{
    
    self.iconImgView = [[UIImageView alloc]init];
    [self.contentView addSubview:self.iconImgView];
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(20);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
    }];
    
    self.titleLb = [[UILabel alloc]init];
    [self.contentView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImgView.mas_bottom).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 12));
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
    }];
    
    _iconImgView.layer.masksToBounds = YES;
    _iconImgView.layer.cornerRadius = 20;
    
    _titleLb.textColor = UIColorFromHex(0x3C3C3C);
    _titleLb.font = PFSC_MediumFont(12);
    _titleLb.textAlignment = NSTextAlignmentCenter;
    
    _iconImgView.backgroundColor = [UIColor greenColor];
    _titleLb.text = @"微信";
}



@end


@interface WKShareView () <UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,copy)void(^block)(NSInteger index);
@end

@implementation WKShareView

+ (void)showWKShareViewComplete:(void(^)(NSInteger index))complete{
    WKShareView *shareView = [[WKShareView alloc]init];
    [shareView show];
    shareView.block = complete;
}

- (instancetype)init{
    if (self == [super init]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, CONTENTHEIGHT)];
    contentView.backgroundColor = [UIColor yellowColor];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(88, 100);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.sectionHeadersPinToVisibleBounds = NO;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(12,0,ScreenWidth-24,200) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:self.collectionView];
    
    _collectionView.layer.masksToBounds = YES;
    _collectionView.layer.cornerRadius = 5;
    
    [self.collectionView registerClass:[WKShareListCell class] forCellWithReuseIdentifier:NSStringFromClass([WKShareListCell class])];
    

    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    _collectionView.layer.masksToBounds = YES;
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(12, 208, ScreenWidth-24, 44);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:UIColorFromHex(0x3C3C3C) forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = PFSC_MediumFont(16);
    cancelBtn.backgroundColor = [UIColor whiteColor];
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.cornerRadius = 5;
    [contentView addSubview:cancelBtn];

    self.contentView = contentView;
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section==0) {
        return 6;
    }
    return 3;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WKShareListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WKShareListCell class]) forIndexPath:indexPath];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.block) {
        self.block(indexPath.row);
    }
}

- (void)show{
    
    [self addSubview:_contentView];
    _contentView.center = self.center;
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    keyWindow.backgroundColor = [UIColor clearColor];
    self.translatesAutoresizingMaskIntoConstraints = NO;
    [keyWindow addSubview:self];
    NSArray *view_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)];
    NSArray *view_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)];
    [keyWindow addConstraints:view_H];
    [keyWindow addConstraints:view_V];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden)];
    [self addGestureRecognizer:tap];
    
    _contentView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, CONTENTHEIGHT+HS_TabbarSafeBottomMargin);
    [UIView animateWithDuration:0.1f animations:^{
         _contentView.frame = CGRectMake(0, ScreenHeight-CONTENTHEIGHT-HS_TabbarSafeBottomMargin, ScreenWidth, CONTENTHEIGHT);
    }];
    
}

- (void)hidden{
    
    [UIView animateWithDuration:0.2f animations:^{
        _contentView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, CONTENTHEIGHT+HS_TabbarSafeBottomMargin);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }];
}


@end
